package com.zzsy.carosshopprojects.view.refreshview;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;

import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.zzsy.carosshopprojects.R;

public class RefreshView extends SmartRefreshLayout {
    private RefreshHelper mRefreshHelper;
    private boolean mEnableRefresh;//是否开启下拉刷新
    private boolean mEnableLoadMore;//是否开启加载更多

    public RefreshView(Context context) {
        super(context);
    }

    public RefreshView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RefreshViewLayout);
        mEnableRefresh = ta.getBoolean(R.styleable.RefreshViewLayout_enableRefresh, true);
        mEnableLoadMore = ta.getBoolean(R.styleable.RefreshViewLayout_enableLoadMore, true);
        ta.recycle();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        setPrimaryColorsId(R.color.white, R.color.systemColor);

        ClassicsHeader classicsHeader = new ClassicsHeader(getContext());
        classicsHeader.setEnableLastTime(false);
        setRefreshHeader(classicsHeader);

        ClassicsFooter classicsFooter = new ClassicsFooter(getContext());
        classicsFooter.setFinishDuration(0);
        setRefreshFooter(classicsFooter);

        setEnableRefresh(mEnableRefresh);
        setEnableLoadMore(mEnableLoadMore);

        setOnRefreshListener(refreshLayout -> mRefreshHelper.onRefresh());
        setOnLoadMoreListener(refreshLayout -> mRefreshHelper.onLoadMore());
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }

    public void finishState() {
        finishRefresh();
        finishLoadMore();
    }



    public interface RefreshHelper {
        void onRefresh();

        void onLoadMore();
    }

    public void setRefreshHelper(RefreshHelper refreshHelper) {
        mRefreshHelper = refreshHelper;
    }
}
